﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 画图
{
    public partial class Form1 : Form
    {
        List<CheckBox> checkBoxs = new List<CheckBox>();
        
        public Form1()
        {
            InitializeComponent();

            var pictureBox = new MyPictureBox();

            pictureBox.Dock = DockStyle.Fill;

            ShowImage2Windows.DrawLines(pictureBox);

            panel1.Controls.Add(pictureBox);

            checkBoxs.Add(cbDraw);
            checkBoxs.Add(cbSelect);
            checkBoxs.Add(cbDelete);
        }

        #region 注释代码
        //private static Bitmap MyImage;
        //public static void ShowMyImage(PictureBox pictureBox, String fileToDisplay, int xSize, int ySize)
        //{
        //    // Sets up an image object to be displayed.
        //    if (MyImage != null)
        //    {
        //        MyImage.Dispose();
        //    }

        //    // Stretches the image to fit the pictureBox.
        //    pictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
        //    MyImage = new Bitmap(fileToDisplay);
        //    pictureBox.ClientSize = new Size(xSize, ySize);
        //    pictureBox.Image = (Image)MyImage;
        //}

        //// 为了方便,直接在一个函数中完成
        //private static void DrawLines(Panel panel,PictureBox pictureBox) {
        //    // 保存绘制点，为了简单，只保存系列个绘制点的坐标，
        //    // 根据需要可以加入系列个绘制点间的颜色及线型
        //    //var points = new List<Point>();
        //    List<LineCoordinates> LinePoints = new List<LineCoordinates>();
        //    // 界面重绘
        //    //panel.Paint += (sender, args) =>
        //    pictureBox.Paint += (sender, args) =>
        //    {
        //        //先添加图片

        //        ShowMyImage(pictureBox, "main.bmp", ((PictureBox)sender).Width, ((PictureBox)sender).Height);

        //        foreach (var line in LinePoints) {
        //            if (line.PointNum >= 2)
        //            { // 绘制出所有绘制点之间的线条,这里为了方便,直接将颜色/线型写入代码中
        //                // 可以根据需要从界面设置中获取参数
        //                if (line.Selected) {//选中
        //                    using (var pen = new Pen(Color.Green, 7)) { //画线
        //                        args.Graphics.DrawLines(pen, line.Points.ToArray());
        //                    }
        //                } else {//没选中
        //                    using (var pen = new Pen(Color.Black, 7)) { //画线
        //                        args.Graphics.DrawLines(pen, line.Points.ToArray());
        //                    }
        //                }
        //            }

        //            if (line.PointNum == (line.StartIndex+1))//做一下限制 ，当只有起始点时，才做虚线
        //            {// 绘制出最后一个与当前鼠标点之间的线条,这里为了方便,直接将颜色/线型写入代码中
        //                // 可以根据需要从界面设置中获取参数
        //                using (var pen = new Pen(Color.Red, 2)){
        //                    var curPostion = panel.PointToClient(Cursor.Position);
        //                    pen.DashStyle = DashStyle.DashDot;
        //                    args.Graphics.DrawLine(pen, line.Points[0], curPostion);
        //                }
        //            }
        //        }

        //    };

        //    // 鼠标移动时直接要求重绘
        //    //panel.MouseMove += (sender, args) =>
        //    pictureBox.MouseMove += (sender, args) =>
        //    {
        //        if (drawFlag == false) return;
        //        panel.Invalidate();//重绘语句
        //    };

        //    // 单击时根据左键或是右键加入绘制点或是清空所有绘制点，然后重绘
        //    /*panel.MouseClick += (sender, args) =>
        //    {
        //        if (args.Button == MouseButtons.Left){
        //            if (drawFlag == false) {
        //                points.Add(new Point(args.X, args.Y));
        //                drawFlag = true;
        //            }
        //        } else if (args.Button == MouseButtons.Right){
        //            if (drawFlag == true) {
        //                points.Add(new Point(args.X, args.Y));
        //                //points.Clear();
        //                drawFlag = false;
        //            }
        //        }
        //        panel.Invalidate();
        //    };*/


        //    //panel.MouseDown += (sender, args) => {
        //    pictureBox.MouseDown += (sender, args) => {
        //        if (args.Button != MouseButtons.Left) return;

        //        foreach (var line in LinePoints) line.Selected = false;

        //        if (drawMode == 0) { 
        //            //开始
        //            LinePoints.Add(new LineCoordinates());
        //            LinePoints[LinePoints.Count - 1].Points[LinePoints[LinePoints.Count - 1].StartIndex] = new Point(args.X, args.Y);
        //            LinePoints[LinePoints.Count - 1].PointNum = 1;
        //            drawFlag = true;
        //        } else if (drawMode == 1 || drawMode == 2) {
        //            foreach (var line in LinePoints) {
        //                bool ret = line.CalcSelectedPoint(new Point(args.X, args.Y));
        //                if (ret == true && drawMode == 2) {
        //                    //((PictureBox)sender).Invalidate();//发送重绘要求
        //                    DialogResult result = ShowMessage("是否删除选中的线？");
        //                    if (result == DialogResult.No)  break; 
        //                    LinePoints.Remove(line);
        //                    break;
        //                } else if (ret == true) {
        //                    //MessageBox.Show("选中了直线！");
        //                    break;
        //                }
        //            }
        //        }

        //        ((PictureBox)sender).Invalidate();
        //    };

        //    //panel.MouseUp += (sender, args) => {
        //    pictureBox.MouseUp += (sender, args) => {

        //        if (args.Button != MouseButtons.Left) return;
        //        if (drawMode != 0) return; //不是绘制模式，不需要继续

        //        //结束
        //        LinePoints[LinePoints.Count - 1].Points[LinePoints[LinePoints.Count - 1].EndIndex] = new Point(args.X, args.Y);
        //        LinePoints[LinePoints.Count - 1].PointNum = 2;
        //        LinePoints[LinePoints.Count - 1].InitKandB();
        //        drawFlag = false;
        //        panel.Invalidate();
        //    };
        //}
        #endregion

        private static DialogResult ShowMessage(string msg) {
            return MessageBox.Show(msg, "提示：", MessageBoxButtons.YesNo);
        }

        private void InitCheckBoxs() {
            foreach (var cb in checkBoxs) cb.Checked = false;
        }

        private void cbDraw_CheckedChanged(object sender, EventArgs e) {

            if(((CheckBox)sender).Checked == false) {
                //((CheckBox)sender).Checked = true;
                return;
            }
            ShowImage2Windows.drawMode = 0;
            ClearCheckBox(ShowImage2Windows.drawMode);
        }

        private void cbSelect_CheckedChanged(object sender, EventArgs e) {
            if (((CheckBox)sender).Checked == false) {
                //((CheckBox)sender).Checked = true;
                return;
            }
            ShowImage2Windows.drawMode = 1;
            ClearCheckBox(ShowImage2Windows.drawMode);
        }

        private void cbDelete_CheckedChanged(object sender, EventArgs e) {
            if (((CheckBox)sender).Checked == false) {
                //((CheckBox)sender).Checked = true;
                return;
            }
            ShowImage2Windows.drawMode = 2;
            ClearCheckBox(ShowImage2Windows.drawMode);
        }

        private void ClearCheckBox(int mode) {
            for (int index = 0; index < checkBoxs.Count; index++) {
                if (index == mode) continue;
                checkBoxs[index].Checked = false;
            }
        }

    }
}
